package io.terminus.spzx.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.terminus.spzx.manager.mapper.ProductDetailsMapper;
import io.terminus.spzx.manager.mapper.ProductMapper;
import io.terminus.spzx.manager.mapper.ProductSkuMapper;
import io.terminus.spzx.manager.service.ProductService;
import io.terminus.spzx.model.dto.product.ProductDto;
import io.terminus.spzx.model.dto.product.SkuSaleDto;
import io.terminus.spzx.model.entity.product.Product;
import io.terminus.spzx.model.entity.product.ProductDetails;
import io.terminus.spzx.model.entity.product.ProductSku;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 商品管理Service接口实现类
 * @Date 2024-10-11
 */
@Service
@RequiredArgsConstructor
public class ProductServiceImpl implements ProductService {

    private final ProductMapper productMapper;

    private final ProductSkuMapper productSkuMapper;

    private final ProductDetailsMapper productDetailsMapper;

    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page, limit);
        List<Product> list = productMapper.findByPage(productDto);
        return new PageInfo<>(list);
    }

    @Override
    public Product getById(Long id) {
        // 1、根据商品id查询商品数据
        Product product = productMapper.selectById(id);

        // 2、根据商品id查询sku数据
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(id);
        product.setProductSkuList(productSkuList);

        // 3、根据商品id查询商品详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        product.setDetailsImageUrls(productDetails.getImageUrls());

        return product;
    }

    @Override
    public void save(Product product) {
        // 1、保存商品
        saveProduct(product);

        // 2、保存商品SKU
        saveProductSku(product);

        // 3、保存商品详情
        saveProductDetail(product);
    }

    @Override
    public void updateById(Product product) {
        // 1、修改商品基本数据
        productMapper.updateById(product);

        // 2、修改商品SKU
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSkuMapper::updateById);

        // 3、修改商品详情
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }

    @Override
    public void deleteById(Long id) {
        // 1、根据商品id删除商品基本数据
        productMapper.deleteById(id);

        // 2、根据商品id删除商品SKU
        productSkuMapper.deleteByProductId(id);

        // 3、根据商品id删除商品详情
        productDetailsMapper.deleteByProductId(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        product.setAuditStatus(auditStatus);
        product.setAuditMessage(auditStatus == 1 ? "审批通过" : "审批不通过");

        productMapper.updateById(product);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);

        productMapper.updateById(product);
    }

    @Override
    public Boolean updateSkuSaleNum(List<SkuSaleDto> skuSaleDtoList) {
        if(CollectionUtils.isNotEmpty(skuSaleDtoList)) {
            for(SkuSaleDto skuSaleDto : skuSaleDtoList) {
                productSkuMapper.updateSale(skuSaleDto.getSkuId(), skuSaleDto.getNum());
            }
        }
        return true;
    }

    /**
     * 保存商品
     *
     * @param product 商品
     */
    private void saveProduct(Product product) {
        // 设置上架状态: 初始值
        product.setStatus(0);
        // 设置审核状态: 初始值
        product.setAuditStatus(0);

        productMapper.save(product);
    }

    /**
     * 保存商品SKU
     *
     * @param product 商品
     */
    private void saveProductSku(Product product) {
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0, size = productSkuList.size(); i < size; i++) {
            ProductSku productSku = productSkuList.get(i);
            // 构建skuCode
            productSku.setSkuCode(product.getId() + "_" + i);
            // 设置商品id
            productSku.setProductId(product.getId());
            productSku.setSkuName(product.getName() + productSku.getSkuSpec());
            // 设置销量
            productSku.setSaleNum(0);
            productSku.setStatus(0);

            productSkuMapper.save(productSku);
        }
    }

    /**
     * 保存商品详情
     *
     * @param product 商品
     */
    private void saveProductDetail(Product product) {
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());

        productDetailsMapper.save(productDetails);
    }
}
